﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Travasto.Model;
using Travasto.Web.Areas.Admin.Commands;
using Travasto.Web.Areas.Admin.ViewModes;
using Travasto.Web.Models;

namespace Travasto.Web.Areas.Admin.Controllers
{
    public class TourController : Controller
    {
        private TravastoDbContext db = new TravastoDbContext();

        //
        // GET: /Tour/

        public ActionResult Index()
        {
            return View(db.Tours.ToList());
        }

        //
        // GET: /Tour/Details/5

        public ActionResult Details(int id = 0)
        {
            Tour tour = db.Tours.Find(id);
            if (tour == null)
            {
                return HttpNotFound();
            }
            return View(new TourViewModel() { Tour = tour });
        }

        //
        // GET: /Tour/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Tour/Create

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Create(TourViewModel tourViewModel)
        {
            if (ModelState.IsValid)
            {
                new AddTourCommand(tourViewModel.Tour, db).Execute();
                return RedirectToAction("Index");
            }

            return View(tourViewModel);
        }

        //
        // GET: /Tour/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tour tour = db.Tours.Include(t => t.Tags).SingleOrDefault(t => t.TourId == id);
            
            if (tour == null)
            {
                return HttpNotFound();
            }
            return View(new TourViewModel() { Tour = tour });
        }

        //
        // POST: /Tour/Edit/5

        [HttpPost]
        //[ValidateAntiForgeryToken]
        public ActionResult Edit(TourViewModel tourViewModel)
        {
            if (ModelState.IsValid)
            {
                new UpdateTourCommand(tourViewModel.Tour, db).Execute();

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tourViewModel);
        }

        //
        // GET: /Tour/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tour tour = db.Tours.Find(id);
            if (tour == null)
            {
                return HttpNotFound();
            }
            return View(new TourViewModel() { Tour = tour });
        }

        //
        // POST: /Tour/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Tour tour = db.Tours.Find(id);
            db.Tours.Remove(tour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}